﻿#include "widget.h"
#include "ui_widget.h"
#include <QtDebug>
#include <QMessageBox>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    add_page = new DatabaseOpreate(this);
    changeWordClass = new QChangeWordClass(this);
    model = new editQueryModel(this);

    ui->tableView->setModel(model);

    ui->tableView->horizontalHeader()->setFont( QFont( "黑体" ) );
    ui->tableView->setAlternatingRowColors(true);
    ui->tableView->horizontalHeader()->setDefaultAlignment( Qt::AlignHCenter|Qt::AlignVCenter );
    ui->tableView->setSelectionBehavior( QAbstractItemView::SelectRows );
    ui->tableView->setSelectionMode( QAbstractItemView::SingleSelection );
    ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
    refreshTableView();
#if 1
    menu = new QMenu( ui->tableView );
    delectRowAct = new QAction("删除当前行", this );
    addRowAct = new QAction("增加行", this );
    menu->addAction(delectRowAct);
#endif
//    menu->addAction(addRowAct);
//    connect( ui->tableView, &QTableView::customContextMenuRequested, this, &Widget::rightclickslot );
//    connect( delectRowAct, &QAction::triggered,this,&Widget::deleteslotrow);
//    connect( addRowAct, &QAction::triggered,this,&Widget::addNewRow);
//    connect( add_page, &DatabaseOpreate::sendNewData, model, &editQueryModel::addData );
//    connect( model, &editQueryModel::sendRefreshTable, this, &Widget::refreshTableView );
//   connect( ui->tableView, &QTableView::doubleClicked, model, &editQueryModel::saveOldData );
//    connect( model, &editQueryModel::addDataFinish, add_page, &DatabaseOpreate::addFinishTrips );
//    connect( ui->tableView, &QTableView::doubleClicked, changeWordClass, &QChangeWordClass::showChangeWordClassSlot );
//    connect( changeWordClass, &QChangeWordClass::sendWordClass, model, &editQueryModel::changeWordClassSlot );

}

Widget::~Widget()
{
    delete ui;
}

void Widget::rightclickslot(QPoint pos)
{
    QModelIndex index = ui->tableView->indexAt(pos);
    currentrow = index.row();
    if(index.isValid())
    {
         menu->exec(QCursor::pos());
    }
    else
    {
        qDebug()<<"index error";
    }
}

void Widget::deleteslotrow()
{
    QMessageBox message(QMessageBox::NoIcon,QString::fromUtf8("提示"),QString::fromUtf8("是否删除本行数据"),
                        QMessageBox::Yes | QMessageBox::No,NULL);
    //如确认删除
    if(message.exec() == QMessageBox::Yes)
    {
        model->deleteData( currentrow );
    }
}

void Widget::addNewRow()
{
    qDebug()<<"add new row";
    qDebug()<<model->rowCount();
}

void Widget::refreshTableView()
{
    ui->tableView->setColumnHidden(0,true);
    ui->tableView->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch ) ;
    ui->tableView->horizontalHeader()->setSectionResizeMode( 2, QHeaderView::Stretch ) ;
    ui->tableView->horizontalHeader()->setSectionResizeMode( 3, QHeaderView::Stretch ) ;
    if ( changeWordClass->currentIndex.isValid() == true )
    {
        ui->tableView->selectRow( changeWordClass->currentIndex.row() );
    }
}

void Widget::on_addBtn_clicked()
{   
//    add_page->exec();
}


